﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Ewk.Runtime.Serialization
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public abstract class DatafieldAttribute : Attribute
    {
        private Type _elementType;

        /// <summary>
        /// Initializes the instance.
        /// </summary>
        protected DatafieldAttribute()
        {
            Occurrence = 1;
            Mandatory = false;
        }

        /// <summary>
        /// Gets or sets the format to use for serialization and deserialization.
        /// </summary>
        public string Format { get; set; }

        /// <summary>
        /// Gets or sets a value that indicates if a field is mandatory or not. The default is that a field is not mandatory.
        /// </summary>
        public bool Mandatory { get; set; }

        /// <summary>
        /// Gets or sets the <see cref="Type"/> of the serializer that is used to serialize or deserialize
        /// the property on which this <see cref="DatafieldAttribute"/> is defined on.
        /// </summary>
        public Type SerializerType { get; set; }

        /// <summary>
        /// Gets or sets the number of times the field occurs within the datarecord.
        /// </summary>
        public int Occurrence { get; set; }

        /// <summary>
        /// Gets or sets the <see cref="PropertyInfo"/> of the property this <see cref="DatafieldAttribute"/> is defined on.
        /// </summary>
        internal PropertyInfo PropertyInfo { get; set; }

        /// <summary>
        /// Serializes the value of the property this <see cref="DatafieldAttribute"/> is defined on.
        /// </summary>
        public abstract string Serialize<TDatarecord>(TDatarecord datarecord);

        /// <summary>
        /// Deserializes a value and sets the value of the property this <see cref="DatafieldAttribute"/> is defined on.
        /// </summary>
        public abstract object Deserialize(string datarecord);

        /// <summary>
        /// Gets the <see cref="Type"/> of the element that needs to be serialized of deserialized.
        /// </summary>
        public Type ElementType
        {
            get
            {
                if (_elementType == null)
                {
                    var propertyType = PropertyInfo.PropertyType;

                    if (propertyType.IsArray && propertyType.HasElementType)
                    {
                        _elementType = propertyType.GetElementType();
                    }
                    else
                    {
                        _elementType = propertyType;
                    }
                }

                return _elementType;
            }
        }

        /// <summary>
        /// Occurs when subscribed on and the sum of lengths of fields in the datarecord definition
        /// is greater than the length of the datarecord definition.
        /// </summary>
        /// <remarks>
        /// When a client has not subscribed on this event and the situation of DatafieldOverflow occurs,
        /// a <see cref="DatafieldOverflowException"/> is trown.
        /// </remarks>
        internal event EventHandler<DatafieldOverflowEventArgs> DatafieldOverflow;

        protected void OnDatafieldOverflow(object sender, string message)
        {
            if (DatafieldOverflow == null)
            {
                throw new DatafieldOverflowException(message);
            }
            
            DatafieldOverflow(sender, new DatafieldOverflowEventArgs(message));
        }

        /// <summary>
        /// Occurs when subscribed on and a mandatory field was empty where it should have had a value.
        /// </summary>
        /// <remarks>
        /// When a client has not subscribed on this event and the situation of DatafieldEmpty occurs,
        /// a <see cref="DatafieldEmptyException"/> is trown.
        /// </remarks>
        internal event EventHandler<DatafieldEmptyEventArgs> DatafieldEmpty;

        protected void OnDatafieldEmpty(object sender, string message)
        {
            if (DatafieldEmpty == null)
            {
                throw new DatafieldEmptyException(message);
            }

            DatafieldEmpty(sender, new DatafieldEmptyEventArgs(message));
        }
    }
}
